package com.lhh.company.website.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhh.company.website.entity.Res;
import com.lhh.company.website.entity.SysDict;
import com.lhh.company.website.entity.SysException;
import com.lhh.company.website.mapper.SysDictMapper;
import com.lhh.company.website.utils.SysUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author lx
* @description 针对表【sys_dict(系统字典表 系统数据字典)】的数据库操作Service实现
* @createDate 2025-07-30 20:39:06
*/
@Service
public class SysDictService extends ServiceImpl<SysDictMapper, SysDict>{

    @Resource
    private SysDictMapper sysDictMapper;

    public Res<ArrayList<SysDict>> getDict() {
        return Res.succ(SysUtils.sysDictList);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public Res<ArrayList<SysDict>> deleteDict(Map<String, String> map) throws SysException {
        List<String> ids = new ArrayList<>();
        String pid = map.get("pid");
        if(pid.equals("-1")){
            throw new SysException("顶层不允许删除");
        }
        SysDict pSysDict = SysUtils.sysDictMap.get(pid);
        List<SysDict> siblingsList = pSysDict.getChildren();
        //从父节点删除
        for (int i = 0; i < siblingsList.size(); i++) {
            String id = siblingsList.get(i).getId();
            if (map.get("id").equals(id)) {
                siblingsList.remove(i);
                break;
            }
        }
        getDeleteAllDictIds(SysUtils.sysDictMap.get(map.get("id")),ids);
        //从数据库删除
        sysDictMapper.deleteByIds(ids);
        for (String id : ids) {
            SysUtils.sysDictMap.remove(id);
        }

        return Res.succ(SysUtils.sysDictList);
    }

    //递归删除节点
    void getDeleteAllDictIds(SysDict sysDict ,List<String> ids ) {

        if(sysDict.getChildren().size()>0){
            for(int i=0;i<sysDict.getChildren().size();i++){
                getDeleteAllDictIds(sysDict.getChildren().get(i),ids);
            }
        }
        ids.add(sysDict.getId());
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public Res<ArrayList<SysDict>> updateOrSaveDict(SysDict sysDict) throws SysException {
        String id = sysDict.getId();
        String pid = sysDict.getPId();
        if(pid.equals("-1")){
            throw new SysException("顶层不允许新增修改");
        }
        SysDict pSysDict = SysUtils.sysDictMap.get(pid);
        sysDict.setChildren(new ArrayList<>());

        List<SysDict> siblingsList = pSysDict.getChildren();
        //保存
        if (StringUtils.isBlank(id)) {
            //新增
            id = SysUtils.getId();
            sysDict.setId(id);

            if(siblingsList.size()==0){
                siblingsList.add(sysDict);
                SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
            }else{
                int maxorderno = siblingsList.getLast().getOrderno();
                if(maxorderno <= sysDict.getOrderno()){
                    siblingsList.add(sysDict);
                    SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
                }else{
                    for (int i = 0; i < siblingsList.size(); i++) {
                        SysDict subSysDict = siblingsList.get(i);
                        if (subSysDict.getOrderno() > sysDict.getOrderno()) {
                            siblingsList.add(i, sysDict);
                            SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
                            break;
                        }
                    }
                }

            }
        } else {
            //修改
            //先从原来的列表删除
            for (int i = 0; i < siblingsList.size(); i++) {
                SysDict subSysDict = siblingsList.get(i);
                if (subSysDict.getId().equals(sysDict.getId())) {
                    siblingsList.remove(subSysDict);
                    SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
                }
            }
            //新的位置添加新的
            int maxorderno = siblingsList.getLast().getOrderno();
            if(maxorderno <= sysDict.getOrderno()){
                siblingsList.add(sysDict);
                SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
            }else{
                for (int i = 0; i < siblingsList.size(); i++) {
                    SysDict subSysDict = siblingsList.get(i);
                    if (subSysDict.getOrderno() > sysDict.getOrderno()) {
                        siblingsList.add(i, sysDict);
                        SysUtils.sysDictMap.put(sysDict.getId(), sysDict);
                        break;
                    }
                }
            }
        }

        sysDictMapper.insertOrUpdate(sysDict);
        return Res.succ(SysUtils.sysDictList);
    }
}




